package com.bootdo.common;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadJson {
	
	private static Map<String,Object> map=new HashMap<String,Object>();
	
    public static void main(String args[]) throws FileNotFoundException {
    	String oldPath="D:\\人标注XML\\人标注XML\\";
    	getXml(oldPath);
    	
    }

    /**
     * 读入TXT文件
     */
    public static void readFile() {
        String pathname = "D:\\人标注XML\\人标注XML\\0AC8B815DE5AFAA4423AD76BE4955CDC.xml"; // 绝对路径或相对路径都可以，写入文件时演示相对路径,读取以上路径的input.txt文件
        String copyPath="D:\\人标注XML\\ceshi\\";
        List<String> list=new ArrayList<String>();
        try (FileReader reader = new FileReader(pathname);
             BufferedReader br = new BufferedReader(reader) // 建立一个对象，它把文件内容转成计算机能读懂的语言
        ) {
            String line;
            //网友推荐更加简洁的写法
            while ((line = br.readLine()) != null) {
            	System.out.println(line);
            	list.add(line);
            }
            writeFile(copyPath+"ceshi.xml",list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入TXT文件
     */
    public static void writeFile(String path,List<String> list) {
        try {
            File writeName = new File(path); // 相对路径，如果没有则要建立一个新的output.txt文件
            writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
            try (FileWriter writer = new FileWriter(writeName);
                 BufferedWriter out = new BufferedWriter(writer)
            ) {
            	if(!list.isEmpty()) {
            		for (String string : list) {
            			out.write(string+"\r\n");
					}
            	}
                out.flush(); // 把缓存区内容压入文件
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
 // 读取txt内容
    public static void txt2String(File file) {
        StringBuilder result = new StringBuilder();
        String name = file.getName();
        String copyPath="D:\\人标注XML\\ceshi\\";
        List<String> list=new ArrayList<String>();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {// 使用readLine方法，一次读一行
            	if(s.contains("<name>rerson") ) {
            		s="\t\t<name>person</name>";
            		list.add(s);
            	}else {
            		list.add(s);
            	}
            }
            writeFile(copyPath+name,list);
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 // 读取文件夹下所有文件名
    public static List<File> getFile(File file) {
        List<File> listLocal = new ArrayList<File>();
        if (file != null) {
            File[] f = file.listFiles();
            if (f != null) {
                for (int i = 0; i < f.length; i++) {
                	File file2 = f[i];
                	String name = file2.getName();
                    getFile(f[i]);
                    listLocal.add(f[i]);
                }
            } else {
                // System.out.println(file);
            }
        }
        return listLocal;
    }

    public static void getXml(String path) throws FileNotFoundException {
            // 文件夹下每一个txt名
            File all = new File(path);
            //将全部txt存到list中
            List<File> allPath = getFile(all);
            for (File ff : allPath) {
                txt2String(ff);
			}
    }


}
